package com.f5.management.service.impl;

import com.f5.management.dao.*;
import com.f5.management.entity.*;
import com.f5.management.service.ParkingManagementService;
import com.f5.management.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ParkingManagementServiceImpl implements ParkingManagementService {
    public static final int PRICE_OF_SELL = 70000;
    public static final int PRICE_OF_RENT = 200;

    @Autowired
    ParkingManagementDAO managementDAO;
    @Autowired
    ParkingDAO parkingDAO;
    @Autowired
    MemberDao memberDao;
    @Autowired
    VehicleDao vehicleDao;
    @Autowired
    UserVehicleDAO uvDAO;
    @Autowired
    CommunityDAO communityDAO;

    @Override
    public List<ParkingManagement> list() {
        List<ParkingManagement> parkingManagements = managementDAO.listNotUse();
        List<ParkingManagement> list = managementDAO.list();
        parkingManagements.addAll(list);
        return parkingManagements;
    }

    @Override
    public ParkingManagement get(int id) {
        return managementDAO.get(id);
    }

    @Override
    public void insert(ParkingManagement parkingManagement) {


        managementDAO.insert(parkingManagement);
    }

    private void updateParking(ParkingManagement parkingManagement, int status) {
        Parking parking = parkingManagement.getParking();
        String serialNumber = parking.getSerialNumber();
        Parking PInDB = parkingDAO.getBySerialNum(serialNumber);

        PInDB.setStatus(status);
        parkingDAO.update(PInDB);
    }

    @Override
    public void update(ParkingManagement parkingManagement) {
        Vehicle vehicle = parkingManagement.getVehicle();
        int status = parkingManagement.getStatus();

        // 闲置车位为“-1未使用”，则车位为“0闲置”
        if (status == -1) {
            this.updateParking(parkingManagement, 0);
        } else {
            this.updateParking(parkingManagement, 1);
        }

        // 更新未使用车位
        if (parkingManagement.getStatus() == -1 && parkingManagement.getVehicle().getId() != 0) {
            parkingManagement.setStatus(-1);
            managementDAO.update(parkingManagement);
            return;
        }

        // 未使用车位处理
        if (parkingManagement.getVehicle().getId() == 0) {
            // 获取管理车位所在的小区
            Parking parking = parkingManagement.getParking();
            String serialNumber = parking.getSerialNumber();
            Parking pkInDB = parkingDAO.getBySerialNum(serialNumber);
            Community community = pkInDB.getCommunity();

            // 小区注入pm
            parkingManagement.setParking(pkInDB);

            Vehicle newVehicle = new Vehicle();

            Member member = vehicle.getMember();
            member.setCommunity(community);

            RealEstate realEstate = new RealEstate();
            realEstate.setId(1);

            member.setRealEstate(realEstate);

            newVehicle.setMember(member);

            memberDao.insertOne(member);

            // 设置车牌
            newVehicle.setNumberPlate(vehicle.getNumberPlate());

            vehicleDao.insertOne(vehicle);

            // 关联uv表
            UserVehicle userVehicle = new UserVehicle();
            userVehicle.setUid(member.getId());
            userVehicle.setVid(vehicle.getId());
            uvDAO.insert(userVehicle);

            parkingManagement.setUserVehicle(userVehicle);

            managementDAO.update(parkingManagement);
            return;
        }

        Parking parking = parkingManagement.getParking();
        Community community = parking.getCommunity();

        String name = community.getName();
        Community byName = communityDAO.getByName(name);
        int cid = byName.getId();
        community.setId(cid);

        vehicle.setCommunity(community);


        vehicleDao.updateVehicleById(vehicle, vehicle.getId());

        // ----->更新车辆数据
        int vid = vehicle.getId();
        Vehicle vInDB = vehicleDao.selectVehicleById(vid);

        // 1.更新车牌号
        vInDB.setNumberPlate(vehicle.getNumberPlate());

        vehicleDao.updateVehicleById(vInDB, vid);

        // ------>更新车主数据
        Member member = vehicle.getMember();
        int uid = member.getId();
        Member mInDB = memberDao.selectMemberById(uid);

        // 1.更新车主姓名
        mInDB.setMemberName(member.getMemberName());

        // 2.更新联系方式
        mInDB.setPhone(member.getPhone());

        memberDao.updateMember(mInDB, uid);

        // 关联uv表
        UserVehicle userVehicle = new UserVehicle();
        userVehicle.setUid(member.getId());
        userVehicle.setVid(vehicle.getId());
        uvDAO.insert(userVehicle);

        parkingManagement.setUserVehicle(userVehicle);

        managementDAO.update(parkingManagement);
    }

    @Override
    public void delete(int id) {
        managementDAO.delete(id);
    }

    @Override
    public Long count() {
        return managementDAO.count();
    }

    @Override
    public void sell(ParkingManagement parkingManagement) {
        Parking parking = parkingManagement.getParking();
        String name = parking.getCommunity().getName();
        List<Parking> parkings = freeParkingInCommunity(name);
        if (parkings.size() == 0) {
            System.out.println("无闲置车位");
            return;
        }

// TODO: 2021/4/8  
        int id = parking.getId();
        ParkingManagement management = managementDAO.get(id);
        // 0--出租，1--出售
        int status = management.getStatus();

        if (status == 0) {
            Date startTime = management.getStartTime();
            Date endTime = management.getEndTime();
            int money = Util.dateToMoney(startTime, endTime);
            management.setCost(money);
        } else {
            management.setStartTime(null);
            management.setEndTime(null);
            management.setCost(PRICE_OF_SELL);
        }
        managementDAO.update(management);
    }

    public List<Parking> freeParkingInCommunity(String communityName) {
        List<Parking> list = parkingDAO.listByCommunity(communityName);
        List<Parking> freeParking = new ArrayList<>();
        for (Parking p : list) {
            if (p.getStatus() == 0) {
                freeParking.add(p);
            }
        }
        return freeParking;
    }

    public List<String> getParkingNumber(String communityName) {
        ArrayList<String> numbers = new ArrayList<>();

        List<Parking> freeParkingInCommunity = freeParkingInCommunity(communityName);

        for (Parking parking : freeParkingInCommunity) {
            String serialNumber = parking.getSerialNumber();
            numbers.add(serialNumber);
        }

        return numbers;
    }

    @Override
    public List<ParkingManagement> findByPage(Integer pageNow, Integer rows) {
        int start = (pageNow - 1) * rows;
        return managementDAO.findByPage(start, rows);
    }
}
